package com.hutiao.snowhb.actions.huodong;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.hutiao.snowhb.dao.BaseDao;
import com.hutiao.snowhb.entities.SignupYctyForm;
import com.hutiao.snowhb.entities.WugangFormBean;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/huodong")
public class SignupAction extends ActionSupport {

	private static final long serialVersionUID = -259847458622852670L;

	private static Logger logger = Logger.getLogger(SignupAction.class);
	
	@Autowired
	private BaseDao baseDao;
	
	private WugangFormBean bean;
	
	private SignupYctyForm yctyBean;
	
	private String result;
	
	@JSON(serialize=false)
	public WugangFormBean getBean() {
		return bean;
	}

	public void setBean(WugangFormBean bean) {
		this.bean = bean;
	}

	public SignupYctyForm getYctyBean() {
		return yctyBean;
	}

	public void setYctyBean(SignupYctyForm yctyBean) {
		this.yctyBean = yctyBean;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
	@Action(value="wugangtuangou")
	public String wugangtuangou() throws Exception {
		
		return SUCCESS;
	}
	
	@Action(value="wugangtuangou_save",results = { @Result(type = "json") })
	public String wugangtuangouSave() throws Exception {
		
		if (this.bean == null) {
			this.result = "请填写姓名";
			return SUCCESS;
		}
		
		if (this.bean.getName() == null || this.bean.getName().equals("")) {
			this.result = "请填写姓名";
			return SUCCESS;
		}
		
		if (this.bean.getPhone() == null || this.bean.getPhone().equals("")) {
			this.result = "请填写电话";
			return SUCCESS;
		}
		
		if (this.bean.getPackage0() == null || this.bean.getPackage0().equals("")) {
			this.result = "请选择购酒种类";
			return SUCCESS;
		}
		
		if (this.bean.getAmount() == null || this.bean.getAmount().equals("")) {
			this.result = "请填写购买箱数";
			return SUCCESS;
		}
		
		
		String sql = "insert into signup_wugang(`name`,phone,package,amount,createTime) "
				+ "values('"+this.bean.getName()+"','"+this.bean.getPhone()+"','"+this.bean.getPackage0()+"','"+this.bean.getAmount()+"', now()); ";
		
		try {
			this.baseDao.execute(sql);
			this.result = "您已成功报名雪花钢城团购活动";
		} catch (Exception e) {
			logger.error("Exception", e);
			
			return ERROR;
		}
		
		
		return SUCCESS;
		
	}
	
	
	@Action(value="yongchuangtianya")
	public String yongchuangtianya() throws Exception {
		
		return SUCCESS;
	}
	
	@Action(value="yongchuangtianya_save",results = { @Result(type = "json") })
	public String yongchuangtianyaSave() throws Exception {
		
		if (this.yctyBean == null) {
			this.result = "请填写姓名";
			return SUCCESS;
		}
		
		if (this.yctyBean.getName() == null || this.yctyBean.getName().equals("")) {
			this.result = "请填写姓名";
			return SUCCESS;
		}
		
		if (this.yctyBean.getGender() == null || this.yctyBean.getGender().equals("")) {
			this.result = "请填写性别";
			return SUCCESS;
		}
		
		if (this.yctyBean.getAge() == null || this.yctyBean.getAge().equals("")) {
			this.result = "请填写年龄";
			return SUCCESS;
		}
		
		if (this.yctyBean.getPhone() == null || this.yctyBean.getPhone().equals("")) {
			this.result = "请填写电话";
			return SUCCESS;
		}
		
		if (this.yctyBean.getArea() == null || this.yctyBean.getArea().equals("")) {
			this.result = "请选择报名区域";
			return SUCCESS;
		}
		
		
		String sql = "insert into signup_ycty(`name`,gender,age,phone,area,createTime) "
				+ "values('"+this.yctyBean.getName()+"','"+this.yctyBean.getGender()+"','"+this.yctyBean.getAge()+"','"+this.yctyBean.getPhone()+"','"+this.yctyBean.getArea()+"', now()); ";
		
		try {
			this.baseDao.execute(sql);
			this.result = "您已成功报名“雪花勇闯天涯 挑战未登峰”";
		} catch (Exception e) {
			logger.error("Exception", e);
			
			return ERROR;
		}
		
		
		return SUCCESS;
		
	}
}
